package com.example.elderlycare.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.elderlycare.controller.entity.NotifyRequestBody;
import com.example.elderlycare.dao.entity.Notify;
import com.example.elderlycare.service.NotifyService;
import com.example.elderlycare.service.entity.NotifyListResponseBody;
import com.example.elderlycare.utils.Result;
import com.example.elderlycare.utils.ResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.Date;

@RestController
@Slf4j
@RequestMapping("/notify")
public class NotifyController {
    @Resource
    NotifyService notifyService;

    /**
     * 查询通知公告
     *
     * @param title     标题
     * @param publisher 发布人
     * @param pageSize  页大小
     * @param current   当前页
     * @return NotifyListResponseBody
     */
    @GetMapping
    public Result<NotifyListResponseBody> queryNotifies(
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "publisher", required = false) String publisher,
            @RequestParam(defaultValue = "10", value = "pageSize") int pageSize,
            @RequestParam(defaultValue = "1", value = "current") int current
    ) {
        QueryWrapper<Notify> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(title != null && !"".equals(title), "title", title)
                .like(publisher != null && !"".equals(publisher), "publisher", publisher);

        try {
            NotifyListResponseBody res = notifyService.queryNotifies(current, pageSize, queryWrapper);
            return Result.success(res);
        } catch (Exception e) {
            log.warn("Exception", e);
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }

    /**
     * 新增通知公告
     *
     * @param req NotifyRequestBody.addNotify
     * @return null
     */
    @PostMapping
    public Result<?> addNotify(@RequestBody NotifyRequestBody.addNotify req) {
        Notify notify = new Notify();
        BeanUtils.copyProperties(req, notify);
        notify.setReleaseTime(new Date());
        try {
            boolean flag = notifyService.save(notify);
            if (flag) {
                return Result.success(null);
            } else {
                log.warn("add Notify error");
                return Result.error(ResultEnum.DEFAULT_ERROR);
            }
        } catch (Exception e) {
            log.warn("Exception", e);
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }

    /**
     * 修改通知公告
     *
     * @param req NotifyRequestBody.updateNotify
     * @return null
     */
    @PutMapping
    public Result<?> updateNotify(@RequestBody NotifyRequestBody.updateNotify req) {
        Notify notify = new Notify();
        BeanUtils.copyProperties(req, notify);
        try {
            boolean flag = notifyService.updateById(notify);
            if (flag) {
                return Result.success(null);
            } else {
                log.warn("update Notify error");
                return Result.error(ResultEnum.DEFAULT_ERROR);
            }
        } catch (Exception e) {
            log.warn("Exception", e);
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }

    /**
     * 删除通知公告记录
     *
     * @param id 记录id
     * @return null
     */
    @DeleteMapping
    public Result<?> deleteNotify(@RequestParam("id") @NotEmpty Integer id) {
        try {
            boolean flag = notifyService.removeById(id);
            if (flag) {
                return Result.success(null);
            } else {
                log.warn("delete Notify error");
                return Result.error(ResultEnum.DEFAULT_ERROR);
            }
        } catch (Exception e) {
            log.warn("Exception", e);
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }
}
